package com.efast.dmom.mgmt.service.mes.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.mes.LotInAndOutBean;
import com.efast.dmom.mgmt.dao.mes.TrBasLotInAndOutDao;
import com.efast.dmom.mgmt.service.mes.TrBasLotInAndOutService;

@Service
public class TrBasLotInAndOutServiceImpl implements TrBasLotInAndOutService {

	@Autowired
	private TrBasLotInAndOutDao trBasLotInAndOutDao;
	
	@Override
	public ResultBean pullOutByLotId(LotInAndOutBean inAndOut) {
		ResultBean result = new ResultBean(true, "'");
		if(inAndOut.getVin() != null || inAndOut.getSelfNumber() != null ) {
			inAndOut.setLotId(inAndOut.getLotId());
			inAndOut.setState("HOLD");
			inAndOut.setInOutStatus("1");
		    inAndOut.setLastUpdateTime(new Date());
		    inAndOut.setLastUpdateUser(SpringWebUtils.getRequestUser());
		    Boolean flag = trBasLotInAndOutDao.updateOutStatusByLotId(inAndOut);
		    if(flag) {
		    	inAndOut.setInOutUser(SpringWebUtils.getRequestUser());
				inAndOut.setInOutTime(new Date());
		        inAndOut.setCreateTime(new Date());
				inAndOut.setCreateUser(SpringWebUtils.getRequestUser());
				inAndOut.setCompanyCode(SpringWebUtils.getRequestCompany());
				trBasLotInAndOutDao.insertInOutByLotId(inAndOut);
				result.setSuccess(true);
				result.setMessage("车辆拉出成功");
		    }else {
		    	result.setSuccess(false);
		    	result.setMessage("车辆拉出失败");
		    }
		} else {
			result.setSuccess(false);
			result.setMessage("请选择VIN号或自编号！");
		}
		return result;
		
	}

	@Override
	public ResultBean takeInByLotId(LotInAndOutBean inAndOutData) {
		ResultBean result = new ResultBean(true, "'");
		if(inAndOutData.getLotId() != null) {
			inAndOutData.setState("NORMAL");
			inAndOutData.setInOutStatus("0");
			inAndOutData.setLastUpdateTime(new Date());
			inAndOutData.setLastUpdateUser(SpringWebUtils.getRequestUser());
			inAndOutData.setStatus("WAITTING");
			Boolean flag = trBasLotInAndOutDao.updateInStatusByLotId(inAndOutData);
		if(flag) {
			inAndOutData.setLastUpdateTime(new Date());
			inAndOutData.setLastUpdateUser(SpringWebUtils.getRequestUser());
			inAndOutData.setIsIn("1");
			trBasLotInAndOutDao.updateInOutByLotId(inAndOutData);
			LotInAndOutBean inData=new LotInAndOutBean();
			inData.setLotId(inAndOutData.getLotId());
			inData.setLotName(inAndOutData.getLotName());
			inData.setVin(inAndOutData.getVin());
			inData.setSelfNumber(inAndOutData.getSelfNumber());
			inData.setStationId(inAndOutData.getStationId());
			inData.setStationName(inAndOutData.getStationName());
			inData.setInOutStatus("0");
			inData.setInOutTime(new Date());
			inData.setInOutUser(SpringWebUtils.getRequestUser());
			inData.setCreateTime(new Date());
			inData.setCreateUser(SpringWebUtils.getRequestUser());
			inData.setCompanyCode(SpringWebUtils.getRequestCompany());
			trBasLotInAndOutDao.insertInOutByLotId(inData);
				result.setMessage("车辆拉入成功");
		    }else {
		    	result.setMessage("车辆拉出失败");
		    }
		}
		
		return result;
	}

}
